<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学号" prop="studentID">
        <el-input
          v-model="queryParams.studentID"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级ID" prop="classID">
        <el-select
          v-model="queryParams.classID"
          style="width: 220px;"
          placeholder="请选择卓越班班级"
          clearable
          @keyup.enter="handleQuery">
          <el-option
            v-for="item in ClassInfoList"
            :key="item.id"
            :label="item.className"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button @click="handleView" icon="Back">返回</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button type="primary" plain icon="Refresh" @click="handleQuery">刷新</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="groupMembersList" @selection-change="handleSelectionChange">
      <el-table-column label="姓名" align="center" prop="stuName" />
      <el-table-column label="学号" align="center" prop="studentId" />
      <el-table-column label="性别" align="center" prop="stuSex" />
      <el-table-column label="班级" align="center" prop="stuClass"/>
      <el-table-column label="卓越班" align="center" prop="classID" >
        <template #default="scope">
          <span>{{CheekClass( scope.row)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="stuGrade" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改小组成员对话框 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form ref="groupmembersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生" prop="studentID">
          <el-input v-model="form.studentID" placeholder="请输入学生ID" style="width: 220px" disabled="disabled"/>
        </el-form-item>
        <el-form-item label="小组" prop="groupID">
          <el-select v-model="form.groupID" placeholder="请输入小组ID" style="width: 220px">
            <el-option
              v-for="item in GroupsListByClass"
              :key="item.groupID"
              :label="item.groupName"
              :value="item.groupID"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="GroupMembers" lang="ts">
import {
  AddGroupMembersByAdmin,
  delGroupMembers,
  NotGroupMembers,
} from '@/apis/system/groupMembers'
import { getCurrentInstance, reactive, ref, toRefs, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listGroups } from '@/apis/system/groups'
import { listClassinfo } from '@/apis/system/classinfo'
import router from '@/router'
import { GroupStore } from '@/stores/group'

const { proxy } = getCurrentInstance() as { proxy: any }

const groupMembersList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref<number[]>([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
  form: {
    groupMemberID: null,
    studentID: null,
    groupID: null,
    classID: null,
    roleInGroup: null,
    joinDate: null
  },
  queryParams: {
    studentID: null,
    classID: null,
  },
  rules: {
    studentID: [
      { required: true, message: '学生ID不能为空', trigger: 'blur' }
    ],
    groupID: [
      { required: true, message: '小组ID不能为空', trigger: 'blur' }
    ]
  }
})
const groupStore=GroupStore()
const { queryParams, form, rules } = toRefs(data)
const GroupsList = ref< any[]>([]);
const GroupsListByClass = ref<any[]>([]);
const GroupsQueryParams= ref({
  pageNum: 1,
  pageSize: 100000000,
  classID: null,
})
listGroups(GroupsQueryParams.value).then(response => {
  if(response.data.code===200){
    GroupsList.value = response.data.data.records
    GroupsListByClass.value=response.data.data.records
  }else {
    ElMessage({ message: response.data.msg, type: 'error'})
  }
})
//如果queryParams.value.classID变化，则根据classID查询小组信息
watch(
  () => queryParams.value.classID,
  () => {
    GroupsListByClass.value=GroupsList.value.filter((item:any)=>{
      if(queryParams.value.classID==null)return true;
      return item.classID===queryParams.value.classID
    })
  }
)
const ClassInfoList = ref< any[]>([]);
const ClassInfoQueryParams= ref({
  pageNum: 1,
  pageSize: 100000000,
})
listClassinfo(ClassInfoQueryParams.value).then(response => {
  ClassInfoList.value = response.data.data.records;
  total.value = response.data.data.total;
});
function CheekClass(row:any) {
  for(let i=0;i<ClassInfoList.value.length;i++){
    if(row.classID===ClassInfoList.value[i].id){
      return ClassInfoList.value[i].className
    }
  }
}
/** 查询小组成员列表 */
function getList() {
  loading.value = true
  NotGroupMembers(queryParams.value).then(response => {
    if(response.data.code===200){
      groupMembersList.value = response.data.data
    }else {
      ElMessage({ message: response.data.msg, type: 'error'})
    }
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  GroupsListByClass.value=GroupsList.value;
  reset()
}

// 表单重置
function reset() {
  form.value = {
    classID: null,
    groupMemberID: null,
    studentID: null,
    groupID: null,
    roleInGroup: null,
    joinDate: null
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    studentID: null,
    classID: null,
  }
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection: any[]) {
  ids.value = selection.map(item => item.groupMemberID)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

function handleView() {
  router.push({ path:  groupStore.groupInfo.OriginGroupPath})
}

/** 修改按钮操作 */
function handleUpdate(row: any) {
  reset()
  GroupsListByClass.value=GroupsList.value.filter((item:any)=>{
    if(row.classID==null)return true;
    return item.classID===row.classID
  })
  form.value.studentID = row.studentId
  form.value.classID = row.classID
  open.value = true
  title.value = '分配小组'
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['groupmembersRef'].validate((valid: boolean) => {
    if (valid) {
      AddGroupMembersByAdmin(form.value).then(response => {
        if(response.data.code===200){
          ElMessage({ message: '修改成功', type: 'success'})
        }else {
          ElMessage({ message: response.data.msg, type: 'error'})
        }
        open.value = false
        GroupsListByClass.value=GroupsList.value;
        getList()
        reset()
      })
    }
  })
}

// /** 导出按钮操作 */
// function handleExport() {
//   proxy.download('system/groupmembers/export', {
//     ...queryParams.value
//   }, `groupmembers_${new Date().getTime()}.xlsx`)
// }

getList()
</script>

<style scoped>

</style>
